require(pacman)
p_load(tidytuesdayR, tidyverse, janitor, here, trashpanda, magick)Salmonid Mortality
Salmon deaths in Norway.
Load Packages
Load Data
tuesdata <- tidytuesdayR::tt_load('2026-03-17')
monthly_losses_data <- tuesdata$monthly_losses_data
monthly_mortality_data <- tuesdata$monthly_mortality_dataPlot
heatmap_data <- monthly_mortality_data |>
mutate(year = year(date),
month = month(date, label = TRUE, abbr = TRUE)) |>
mutate(mortality_anomaly = median - mean(median),
.by = species) |>
mutate(species = case_when(species == "rainbowtrout" ~ "Rainbow Trout",
species == "salmon" ~ "Salmon"))
plot <- heatmap_data |>
ggplot() +
geom_tile(aes(y = fct_rev(month), x = factor(year), fill = mortality_anomaly),
colour = "black", linewidth = 0.1) +
facet_wrap(~species) +
scale_fill_gradient2(
low = "blue",
mid = "white",
high = "red",
midpoint = 0,
name = "Deviation from\nmean (%)") +
labs(x = "Year", y = "Month",
title = "Salmon Confidential: Norwegian Salmon Mortality",
subtitle = str_wrap("Based on data from the Norwegian Veterinary Institute, the majority of anomolous excess death occurs from January to March for Salmon, but later in the year for Rainbow Trout with 2022 being a particularly bad year for Rainbow Trout.")) +
theme_cole(remove_grid = TRUE, base_size = 16) +
add_caption_cwb() +
theme(plot.title = element_text(hjust = 0, colour = "salmon"),
plot.subtitle = element_text(hjust = 0, colour = "salmon"),
legend.position = "right",
legend.key.height = unit(1.2, "cm"),
legend.key.width = unit(0.8, "cm"))
# Save and display images
current_dir <- dirname(knitr::current_input())
plot_name <- "salmon_mortality.png"
ggsave(plot = plot,
dpi = "screen",
width = 16,
height = 14,
device = ragg::agg_png,
filename = file.path(current_dir, plot_name))
# Read the big plot
img <- image_read(file.path(current_dir, plot_name))
# Force 16:9 aspect ratio with minimal padding
# Target size: 1200x675 px (16:9)
img_card <- image_scale(img, "1200x675") # scale to fit inside 16:9
img_card <- image_extent(
img_card,
geometry = "1200x675",
gravity = "center"
)
# Save as card preview
image_write(img_card, path = file.path(current_dir, "preview.png"))
knitr::include_graphics(
file.path(current_dir, plot_name)
)
References
cite_packages(format = "rmd")Ooms J (2025). magick: Advanced Graphics and Image-Processing in R. doi:10.32614/CRAN.package.magick https://doi.org/10.32614/CRAN.package.magick, R package version 2.9.0, https://CRAN.R-project.org/package=magick.
Baril C (????). trashpanda: Cole’s Personal Collection of R Functions, Themes, and Palettes. R package version 0.0.1, https://colebaril.github.io/trashpanda/.
Müller K (2025). here: A Simpler Way to Find Your Files. doi:10.32614/CRAN.package.here https://doi.org/10.32614/CRAN.package.here, R package version 1.0.2, https://CRAN.R-project.org/package=here.
Firke S (2024). janitor: Simple Tools for Examining and Cleaning Dirty Data. doi:10.32614/CRAN.package.janitor https://doi.org/10.32614/CRAN.package.janitor, R package version 2.2.1, https://CRAN.R-project.org/package=janitor.
Grolemund G, Wickham H (2011). “Dates and Times Made Easy with lubridate.” Journal of Statistical Software, 40(3), 1-25. https://www.jstatsoft.org/v40/i03/.
Wickham H (2025). forcats: Tools for Working with Categorical Variables (Factors). doi:10.32614/CRAN.package.forcats https://doi.org/10.32614/CRAN.package.forcats, R package version 1.0.1, https://CRAN.R-project.org/package=forcats.
Wickham H (2025). stringr: Simple, Consistent Wrappers for Common String Operations. doi:10.32614/CRAN.package.stringr https://doi.org/10.32614/CRAN.package.stringr, R package version 1.6.0, https://CRAN.R-project.org/package=stringr.
Wickham H, François R, Henry L, Müller K, Vaughan D (2023). dplyr: A Grammar of Data Manipulation. doi:10.32614/CRAN.package.dplyr https://doi.org/10.32614/CRAN.package.dplyr, R package version 1.1.4, https://CRAN.R-project.org/package=dplyr.
Wickham H, Henry L (2026). purrr: Functional Programming Tools. doi:10.32614/CRAN.package.purrr https://doi.org/10.32614/CRAN.package.purrr, R package version 1.2.1, https://CRAN.R-project.org/package=purrr.
Wickham H, Hester J, Bryan J (2025). readr: Read Rectangular Text Data. doi:10.32614/CRAN.package.readr https://doi.org/10.32614/CRAN.package.readr, R package version 2.1.6, https://CRAN.R-project.org/package=readr.
Wickham H, Vaughan D, Girlich M (2025). tidyr: Tidy Messy Data. doi:10.32614/CRAN.package.tidyr https://doi.org/10.32614/CRAN.package.tidyr, R package version 1.3.2, https://CRAN.R-project.org/package=tidyr.
Müller K, Wickham H (2026). tibble: Simple Data Frames. doi:10.32614/CRAN.package.tibble https://doi.org/10.32614/CRAN.package.tibble, R package version 3.3.1, https://CRAN.R-project.org/package=tibble.
Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4, https://ggplot2.tidyverse.org.
Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R, Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller E, Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi K, Vaughan D, Wilke C, Woo K, Yutani H (2019). “Welcome to the tidyverse.” Journal of Open Source Software, 4(43), 1686. doi:10.21105/joss.01686 https://doi.org/10.21105/joss.01686.
Harmon J, Hughes E (2025). tidytuesdayR: Access the Weekly ‘TidyTuesday’ Project Dataset. doi:10.32614/CRAN.package.tidytuesdayR https://doi.org/10.32614/CRAN.package.tidytuesdayR, R package version 1.2.1, https://CRAN.R-project.org/package=tidytuesdayR.
Rinker TW, Kurkiewicz D (2018). pacman: Package Management for R. version 0.5.0, http://github.com/trinker/pacman.